package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.OrganizationMapper;
import cc.rengu.igas.bomp.common.entity.Organization;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构信息表操作接口实现类
 * Created by 王成 on 2020/3/10
 */
public class OrganizationMapperImpl implements OrganizationMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertOrganization(Organization organization) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertOrganization");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, null);
        if (0 != iReturnCode) {
            rglog.error("登记机构信息失败,orgCode:<{}>,orgName:<{}>,id:<{}>,iReturnCode:<{}>",
                    organization.getOrgCode(), organization.getOrgName(), organization.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateOrganizationById(Organization organization) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateOrganizationById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, null);
        if (0 != iReturnCode) {
            rglog.error("登记机构信息失败,orgCode:<{}>,orgName:<{}>,id:<{}>,iReturnCode:<{}>",
                    organization.getOrgCode(), organization.getOrgName(), organization.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public Organization selectOrganizationById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrganizationById");
        Organization organization = new Organization();
        organization.setInstId(instId);
        organization.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (0 != iReturnCode) {
            rglog.error("获取机构信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return organization;
    }

    @Override
    public Organization selectOrganizationByPrimaryKey(String instId, String orgCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrganizationByPrimaryKey");
        Organization organization = new Organization();
        organization.setInstId(instId);
        organization.setOrgCode(orgCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (0 != iReturnCode) {
            rglog.error("获取机构信息失败,instId:<{}>,orgCode:<{}>,iReturnCode:<{}>", instId, orgCode, iReturnCode);
            return null;
        }
        return organization;
    }

    @Override
    public List<Organization> selectOrganizationByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrganizationByInstId");
        Organization organization = new Organization();
        organization.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, organization, Organization.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取机构信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (Organization) item).collect(Collectors.toList());
    }

    @Override
    public List<Organization> selectOrgInfoByorgCode(String instId, String orgCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompOrgInfoByorgCode");
        Organization organization = new Organization();
        organization.setInstId(instId);
        organization.setOrgCode(orgCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, organization, Organization.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取用户所在机构下级机构信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (Organization) item).collect(Collectors.toList());
    }

    @Override
    public List<Organization> selectOrgInfoByInstCodeAndInstName(String instId, String superOrgCode, String instCode, String instName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr = null;
        Organization organization = new Organization();
        if (StringUtil.isEmptyOrNull(instCode) && StringUtil.isEmptyOrNull(instName)) {
            //无查询条件
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompOrgInfoByorgCode");
            organization.setInstId(instId);
            organization.setOrgCode(superOrgCode);
        } else {
            //有查询条件
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompOrgInfoByInstCodeAndInstName");
            organization.setInstId(instId);
            if (!StringUtil.isEmptyOrNull(instCode)) {
                organization.setOrgCode(instCode);
            }
            if (!StringUtil.isEmptyOrNull(instName)) {
                organization.setOrgName(instName);
            }
            organization.setSuperOrgCode(superOrgCode);
        }

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, organization, Organization.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取用户所在机构下级机构信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (Organization) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectOrgInfoByPageAndInstCodeAndInstName(String instId, String superOrgCode, String instCode, String instName, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr = null;
        Organization organization = new Organization();
        if (StringUtil.isEmptyOrNull(instCode) && StringUtil.isEmptyOrNull(instName)) {
            //无查询条件
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompOrgInfoByorgCode");
            organization.setInstId(instId);
            organization.setOrgCode(superOrgCode);
        } else {
            //有查询条件
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompOrgInfoByInstCodeAndInstName");
            organization.setInstId(instId);
            if (!StringUtil.isEmptyOrNull(instCode)) {
                organization.setOrgCode(instCode);
            }
            if (!StringUtil.isEmptyOrNull(instName)) {
                organization.setOrgName(instName);
            }
            organization.setSuperOrgCode(superOrgCode);
        }

        return dbsUtil.dbsFetchPageListData(sqlStr, organization, Organization.class, pageInfo);
    }
}
